package ccb.ygqd.inspur.finger;


import android.content.Context;
import android.os.Bundle;

import com.siecomzg.constconfig.ErrorDefine;
import com.siecomzg.tool.LogUtil;
import com.siecomzg.transport.DeviceCallback;
import com.siecomzg.transport.SiecomDevice;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import ccb.ygqd.inspur.common.ConstHF;

public class finger  {

    private Context mContext;
    private static final String TAG = finger.class.getSimpleName();
    public finger(Context context){
        mContext = context;
    }
    /**
     *
     * @param strTimeout
     * @return 2)如果aryRet[0]为DRV0000000，则aryRet[1]为返回的指纹仪厂商代码，和strCompanyCode的值相同，arrRet[2]为采集的指纹特征码；
     */
    public String[] registerFinger(String strTimeout){
        LogUtil.e(TAG,"registerFinger ---- " + strTimeout);
        final String ret[] = new String[]{"","",""};
        if (!SiecomDevice.isConnected){
            ret[0] = ConstHF.ERR_NOT_CONN;
            ret[1] = ConstHF.ERR_DEV_ONT_CONN;
            return ret;
        }
        int timeOut = Integer.parseInt(strTimeout);
        final CountDownLatch latch = new CountDownLatch(1);
        SiecomDevice.readFingerTempleteRegist(new DeviceCallback() {
            @Override
            public void onStart() {

            }

            @Override
            public void onError(Bundle bundle) {
                int code = bundle.getInt("code");

                if (code == ErrorDefine.FINGERPRINT_FREATURE_TIMEOUT)
                    ret[0] = ConstHF.ERR_TIME_OUT;

                else if(code == ErrorDefine.FINGERPRINT_CANCEL){
                    ret[0] = ConstHF.ERR_USER_CANCEL;
                }else{
                    ret[0] = ConstHF.ERR_HARDWARE;
                }
                ret[1] = bundle.getString("message");
                latch.countDown();
            }

            @Override
            public void onSuccess(Bundle bundle) {

                ret[0] = ConstHF.ERR_SUCCESS;
                try {
                    JSONObject jbin = new JSONObject(bundle.getString("data"));
                    String factory = jbin.getString("factory");
                    if (factory.startsWith("SM"))
                        ret[1] = "3";
                    else
                        ret[2] = "4";

                    ret[2] = jbin.getString("feature");

                } catch (JSONException e) {
                    e.printStackTrace();
                }


                latch.countDown();
            }

            @Override
            public void onCompleted(Bundle bundle) {

            }
        },timeOut*1000,false);
        try {
            latch.await(timeOut + 2, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return ret;
    }

    /**
     * 实时采集的指纹特征信息，将和指纹模板作指纹验证。
     * @param strTimeout 默认20秒
     * @return 2)如果aryRet[0]为DRV0000000，则aryRet[1]为返回的指纹仪厂商代码，和strCompanyCode的值相同，arrRet[2]为采集的指纹特征码；
     */
    public String[] readFinger(String strTimeout){
        LogUtil.e(TAG,"readFinger ---- " + strTimeout);
        final String ret[] = new String[]{"","",""};
        if (!SiecomDevice.isConnected){
            ret[0] = ConstHF.ERR_NOT_CONN;
            ret[1] = ConstHF.ERR_DEV_ONT_CONN;
            return ret;
        }
        int timeOut = Integer.parseInt(strTimeout);
        final CountDownLatch latch = new CountDownLatch(1);
        SiecomDevice.readFingerFeature(new DeviceCallback() {
            @Override
            public void onStart() {

            }

            @Override
            public void onError(Bundle bundle) {
                int code = bundle.getInt("code");

                if (code == ErrorDefine.FINGERPRINT_FREATURE_TIMEOUT)
                    ret[0] = ConstHF.ERR_TIME_OUT;

                else if(code == ErrorDefine.FINGERPRINT_CANCEL){
                    ret[0] = ConstHF.ERR_USER_CANCEL;
                }else{
                    ret[0] = ConstHF.ERR_HARDWARE;
                }
                ret[1] = bundle.getString("message");
                latch.countDown();
            }

            @Override
            public void onSuccess(Bundle bundle) {

                ret[0] = ConstHF.ERR_SUCCESS;
                try {
                    JSONObject jbin = new JSONObject(bundle.getString("data"));
                    String factory = jbin.getString("factory");
                    if (factory.startsWith("SM"))
                        ret[1] = "3";
                    else
                        ret[2] = "4";

                    ret[2] = jbin.getString("feature");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                latch.countDown();
            }

            @Override
            public void onCompleted(Bundle bundle) {

            }
        },timeOut*1000,false);
        try {
            latch.await(timeOut + 2, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return ret;
    }

}
